<?php

namespace App\Http\Controllers\CmsApi;

use App\Exceptions\ErrorCodeException;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\CoolNumberShoppingMall\CoolNumberServices;
use App\Http\Services\CoolNumberShoppingMall\CoolNumberTagServices;
use App\Http\Services\CoolNumberShoppingMall\ShoppingMallGoodsServices;
use App\Http\Services\CoolNumberShoppingMall\ShoppingMallOrderServices;
use Illuminate\Http\Request;

class CoolNumberController extends BaseController
{
    /**
     * @var CoolNumberServices
     */
    private $serviceCoolNumber;

    /**
     * @var ShoppingMallOrderServices
     */
    private $serviceOrder;

    /**
     * @var ShoppingMallGoodsServices
     */
    private $serviceGoods;

    /**
     * @var CoolNumberTagServices
     */
    private $serviceTag;

    /**
     * @param CoolNumberServices $cns
     * @param ShoppingMallOrderServices $order
     * @param ShoppingMallGoodsServices $goods
     * @param CoolNumberTagServices $tag
     */
    public function __construct(CoolNumberServices        $cns, ShoppingMallOrderServices $order,
                                ShoppingMallGoodsServices $goods, CoolNumberTagServices $tag)
    {
        $this->serviceCoolNumber = $cns;
        $this->serviceOrder = $order;
        $this->serviceGoods = $goods;
        $this->serviceTag = $tag;
    }

    /**
     * Created at 2021/12/9 13:19
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws ErrorCodeException
     * @throws \App\Exceptions\PublicException
     */
    public function changeUserUuid(Request $request)
    {
        $oldUuid = $request->input('old_uuid');
        $newUuid = $request->input('uuid');
        $endDate = $request->input('end_date');
        $operatorUserId = $request->input('operator_user_id');
        $operatorUser = $request->input('operator_user');
        if (!$oldUuid || !$newUuid || !$operatorUser || !$operatorUserId) {
            //throw new ErrorCodeException(Hint::PARAM_ERROR);
        }

        $this->serviceOrder->changeUserUuidFromAdmin($oldUuid, $newUuid, $endDate, $operatorUserId, $operatorUser);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * Created at 2021/12/9 15:09
     * @Author <yuxiang.zhu@asiainnovations.net>
     */
    public function getTypes()
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceCoolNumber->getTypes());
    }

    /**
     * 靓号列表
     *
     * Created at 2021/12/9 15:17
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function listAllCoolNumber(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceCoolNumber->search($request));
    }

    /**
     * 靓号使用日志
     *
     * Created at 2021/12/9 15:18
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function listUuidUseLog(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceCoolNumber->listUseLog($request));
    }

    /**
     * 查询用户UUID变更日志
     *
     * Created at 2021/12/9 15:24
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function listUuidUserUseLog(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceCoolNumber->listUuidUserUseLog($request));
    }

    /**
     * 添加靓号
     *
     * Created at 2021/12/9 15:32
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws \App\Exceptions\PublicException
     */
    public function addCoolNumber(Request $request)
    {
        $this->serviceCoolNumber->add($request->input('uuid', ''));
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * cms后台-靓号商品列表
     *
     * Created at 2021/12/9 15:39
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function listGoods(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceGoods->search($request));
    }

    /**
     * 添加/编辑靓号商品
     *
     * Created at 2021/12/9 16:35
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws \App\Exceptions\PublicException
     */
    public function editGoods(Request $request)
    {
        $rules = [
            'id' => 'nullable',
            'goods_name' => 'required|max:255',
            'goods_image' => 'required',
            'is_show' => 'required',
            'weight' => 'required',
            'cool_number_id' => 'required|exists:cool_number,id',
            'effective_time' => 'required',
            'get_description' => 'nullable',
            'tag_id' => 'nullable',
            'can_buy' => 'required',
            'jump_url' => 'nullable',
            'price' => 'required',
            'level' => 'integer',
        ];
        $messages = [
            'goods_name.required' => '请输入商品名称',
            'is_show.required' => '请选择是否展示',
            'weight.required' => '缺少参数：id',
            'goods_image.required' => '请上传大图',
            'weight.integer' => '展示权重必须为正整数',
            'cool_number_id.required' => '请选择靓号',
            'cool_number_id.in' => '靓号不存在',
            'effective_time.required' => '请选择生效时间',
            'can_buy.required' => '请选择是否可购买',
            'price.required' => '请输入积分价格',
        ];
        $params = parent::validateParams($request->all(), $rules, $messages);
        $params['jump_url'] = $params['jump_url'] ?? '';
        $params['get_description'] = $params['get_description'] ?? '';
        $params['tag_id'] = $params['tag_id'] ?? 0;
        $params['level'] = (int)$params['level'];
        $this->serviceGoods->addAndUpdate($params);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 获取未被使用的靓号
     *
     * Created at 2021/12/9 16:45
     * @Author <yuxiang.zhu@asiainnovations.net>
     */
    public function listNotUseCoolNumber()
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceCoolNumber->listNotUseCoolNumber());
    }

    /**
     * 获取生效时间
     *
     * Created at 2021/12/9 19:16
     * @Author <yuxiang.zhu@asiainnovations.net>
     */
    public function getEffectiveTime()
    {
        OutJson::outJsonResult(Hint::SUCCESS, [7, 15, 30, 90, 180, 365, 730]);
    }

    /**
     * Created at 2021/12/10 10:53
     * @Author <yuxiang.zhu@asiainnovations.net>
     */
    public function listTag()
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceTag->getAllTags());
    }

    /**
     * Created at 2021/12/10 18:47
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     */
    public function addAndUpdateTag(Request $request)
    {
        $this->serviceTag->addAndUpdate($request->all());
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * Created at 2021/12/13 14:47
     * @Author <yuxiang.zhu@asiainnovations.net>
     */
    public function listUserCanBuyGoods()
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->serviceGoods->listUserCanBuyGoods());
    }

    /**
     * Created at 2021/12/13 15:47
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param Request $request
     * @throws ErrorCodeException
     * @throws \App\Exceptions\PublicException
     */
    public function give(Request $request)
    {
        $goodsId = intval($request->input('goods_id', 0));// 商品id
        $uuid = $request->input('uuid');// 用户的uuid
        if (!$goodsId || !$uuid) {
            throw new ErrorCodeException(Hint::PARAM_ERROR);
        }

        $this->serviceOrder->giveCoolNumber($goodsId, $uuid);
        OutJson::outJsonResult(Hint::SUCCESS);
    }
}
